package com.cskaoyan.controller;

import com.cskaoyan.annotation.LogRecord;
import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.User;
import com.cskaoyan.bean.order.Order;
import com.cskaoyan.bean.order.OrderData;
import com.cskaoyan.bean.order.OrderGoods;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.service.OrderService;
import com.cskaoyan.service.UserService;
import lombok.Data;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * 用于处理订单模块的请求
 */
@RestController
@RequestMapping("admin/order")
public class OrderController {

    @Autowired
    OrderService orderService;
    @Autowired
    UserService userService;

    @RequiresPermissions(value = {"admin:order:list"})
    @GetMapping("list")
    @LogRecord
    public BaseRespVo<OrderData> list(BaseParam param, Integer[] orderStatusArray, String start, String end, String userId, String orderSn) {
        //接受进来的参数有些需要转成integer,需要判断参数合法性
        Integer userIdInt = null;
        Integer orderSnInt = null;
        try {
            if (userId != null) {
                userIdInt = Integer.parseInt(userId);
            }
            if (orderSn != null) {
                orderSnInt = Integer.parseInt(orderSn);
            }
        } catch (Exception e) {
            return BaseRespVo.invalidData("参数不合法");
        }
        OrderData orderData = orderService.queryOrders(param, orderStatusArray, start, end, userIdInt, orderSnInt);
        return BaseRespVo.ok(orderData);
    }

    @GetMapping("detail")
    @LogRecord
    public BaseRespVo<Map<String, Object>> detail(Integer id) {
        HashMap<String, Object> map = new HashMap<>();
        List<OrderGoods> orderGoods = orderService.queryOrderGoods(id);
        map.put("orderGoods", orderGoods);
        Order order = orderService.queryOrderById(id);
        map.put("order", order);
        User user = userService.queryNicknameAndAvatarById(order.getUserId());
        map.put("user", user);
        return BaseRespVo.ok(map);
    }

    // @PostMapping("refund")
    // public BaseRespVo refund(){
    //
    // }

    /**
     * post json:{"orderId":315,"shipChannel":"ZTO","shipSn":"20220604212422"}
     * resp json:{"errno":0,"errmsg":"成功"}
     */
    @RequiresPermissions(value = {"admin:order:ship"})
    @PostMapping("ship")
    @LogRecord
    public BaseRespVo ship(@RequestBody Map map) {
        Integer orderId = (Integer) map.get("orderId");
        String shipChannel = (String) map.get("shipChannel");
        String shipSn = (String) map.get("shipSn");
        Integer updateShip = orderService.updateShip(orderId, shipChannel, shipSn);
        if (updateShip == 0) {
            return BaseRespVo.invalidData("订单不存在");
        }
        BaseRespVo respVo = BaseRespVo.ok(null);
        respVo.setErrmsg("成功");
        return respVo;
    }

    @Data
    class Channel {
        String code;
        String name;

        public Channel() {
        }

        public Channel(String code, String name) {
            this.code = code;
            this.name = name;
        }
    }

    @GetMapping("channel")
    @LogRecord
    public BaseRespVo channel() {
        LinkedList<Channel> channels = new LinkedList<>();
        Channel channel1 = new Channel("ZTO", "中通快递");
        channels.addLast(channel1);
        Channel channel2 = new Channel("YTO", "圆通速递");
        channels.addLast(channel2);
        Channel channel3 = new Channel("YD", "韵达速递");
        channels.addLast(channel3);
        Channel channel4 = new Channel("YZPY", "邮政快递包裹");
        channels.addLast(channel4);
        Channel channel5 = new Channel("EMS", "EMS");
        channels.addLast(channel5);
        Channel channel6 = new Channel("DBL", "德邦快递");
        channels.addLast(channel6);
        Channel channel7 = new Channel("FAST", "快捷快递");
        channels.addLast(channel7);
        Channel channel8 = new Channel("ZJS", "宅急送");
        channels.addLast(channel8);
        Channel channel9 = new Channel("TNT", "TNT快递");
        channels.addLast(channel9);
        Channel channel10 = new Channel("UPS", "UPS");
        channels.addLast(channel10);
        Channel channel11 = new Channel("DHL", "DHL");
        channels.addLast(channel11);
        Channel channel12 = new Channel("FEDEX", "FEDEX联邦(国内件)");
        channels.addLast(channel12);
        Channel channel13 = new Channel("FEDEX_GJ", "FEDEX联邦(国际件)");
        channels.addLast(channel13);
        return BaseRespVo.ok(channels);
    }

    @PostMapping("refund")
    @LogRecord
    public BaseRespVo refund(@RequestBody Map map) {
        Integer orderId = null;
        Integer refundMoney = null;
        try {
            orderId = (Integer) map.get("orderId");
            refundMoney = (Integer) map.get("refundMoney");
        } catch (Exception e) {
            return BaseRespVo.invalidData("参数不合法");
        }
        Integer updateRefund = orderService.updateRefund(orderId, refundMoney);
        if (updateRefund==0){
            return BaseRespVo.invalidData("订单不存在");
        }
        BaseRespVo respVo = BaseRespVo.ok(null);
        respVo.setErrmsg("成功");
        return respVo;
    }

    @PostMapping("delete")
    @LogRecord
    public BaseRespVo delete(@RequestBody Map map) {
        Integer orderId = (Integer) map.get("orderId");
        try {
            Integer delete = orderService.delete(orderId);
        } catch (Exception e) {
            return BaseRespVo.invalidData("订单不存在,删除失败");
        }
        return BaseRespVo.okAndSuccess(null);
    }
}
